# 2391. 收集垃圾的最少总时间
from typing import List


def garbageCollection(garbage: List[str], travel: List[int]) -> int:
    # 根据对应的类型，收集垃圾
    def garbage_by_type(type: str, garbage_list: List[str]):
        count = 0
        current_travel = 0
        last_travel = 0
        for index, value in enumerate(garbage_list):
            # 当前行驶的距离
            current_travel = current_travel + (0 if index == 0 else travel[index - 1])
            # 转换为str类型
            str_value = str(value)
            # 判断是否有垃圾
            word_count = str_value.count(type)
            # 找到了垃圾
            if word_count > 0:
                count = count + current_travel - last_travel + word_count
                new_value = str_value.replace(type, '')
                garbage_list[index] = new_value
                last_travel = current_travel


        return count

    # 收集对应的垃圾
    _p_collect = garbage_by_type("P", garbage)
    _g_collect = garbage_by_type("G", garbage)
    _m_collect = garbage_by_type("M", garbage)

    return _g_collect + _p_collect + _m_collect


garbage_1 = ["G", "P", "GP", "GG"]
travel_1 = [2, 4, 3]
result = garbageCollection(garbage_1, travel_1)
print(f"result:{result}")

garbage_2 = ["MMM", "PGM", "GP"]
travel_2 = [3, 10]
result = garbageCollection(garbage_2, travel_2)
print(f"result:{result}")
